home *** CD-ROM | disk | FTP | other *** search
/ Nebula 1 / Nebula One.iso / Financial / Amortize1.x / Source / UnixSource / amortize.c
C/C++ Source or Header  |  1992-06-13  |  3KB  |  131 lines

  1. /*
  2.  *
  3.  * loan.c
  4.  * amortization program
  5.  * purpose: calculates monthly payment, future value
  6.  *            and creates an amortization schedule
  7.  *
  8.  * 06/27/84        Bill Gregg, Informatics General Corp.
  9.  * 07/12/84         Revision 1
  10.  * 07/12/84        Revision 2
  11.  * 11/05/85       Jane Medefesser, Implementation NPSN Unix (wilbur)
  12.  *                   compile as follows: 'cc -o <outfile> loan.c -lm'
  13.  * 12/05/85       Changes to direct output to a file.
  14.  * 03/02/88       Implemented on Eternity 5.3.1
  15.  * June 7, 1992  NeXT vanilla c version, Eric Tremblay (NeXTstep version available)
  16.  */
  17.  
  18. #include <stdio.h>
  19. #include <math.h>
  20.  
  21.  
  22. main()         /* loan program */
  23. {
  24.      /* Declare the variables */
  25.     float amt;  /* Input for Principal */
  26.     float rate; /* Input for Interest Rate */
  27.     float mbeg; /* Input for the beginning month */
  28.     
  29.     float ic; /* Interest Payed */
  30.     float pmt; /* Payment */
  31.     float fv; /* Total Cost */
  32.     float term; /* # of months for the term */
  33.  
  34.     
  35.     float r, temp;
  36.     float exp, prin, x, y, yrint = 0;
  37.     int mnbr;
  38.     int month, i, k, a = 0, yr=1;
  39.     
  40.     char d, filename[9], c;
  41.     FILE *fp;
  42.  
  43.            printf("\n       *** Amortize ***\n");
  44.        printf("  The loan amortization tool.\n");
  45.        printf("    NeXT Unix Version 1.0\n\n");
  46.       
  47.        
  48.     /*  Prompt for input from terminal  */
  49.     printf("Enter principal: ");
  50.     scanf("%f", &amt);
  51.  
  52.     printf("Enter term in years: ");
  53.     scanf("%f", &term);
  54.  
  55.     printf("Enter interest rate in percent: ");
  56.     scanf("%f", &rate);
  57.  
  58.     printf("Enter month payments begin (ex: 8 = August): ");
  59.     scanf("%f", &mbeg);
  60.  
  61.     
  62.     /*  compute payment and future value  */
  63.  
  64.     r = rate/100.0;
  65.     x = 1.0 + r/12.0;
  66.     y = term*12.0;
  67.     temp = (1.0 / pow(x,y));
  68.     pmt = (amt*r/12.0) / (1-temp);
  69.     k = term*12;
  70.     fv = pmt*k;
  71.     ic = fv - amt;
  72.  
  73.     printf("Do you want the report directed to a file or to the screen?");
  74.     printf("\n[f = file / s = screen] : ");
  75.     d = getchar();      /* This is only here to eat up the '\n' left over
  76.                    from the last scanf. */
  77.     d = getchar();
  78.     switch(d) {
  79.          case 'f':
  80.          d = getchar();
  81.          printf("\nEnter a filename: ");
  82.          scanf("%s", filename);
  83.          while((c = getchar()) != '\n') {
  84.          filename[a] = c; a++; }
  85.          fp = fopen(filename, "w");
  86.          break;
  87.          default:
  88.          fp = stdout;
  89.          break;
  90.          }
  91.  
  92.          /*  print header  */
  93.            fprintf(fp,"\n   *** Amortize ***\n");
  94.        fprintf(fp,"The loan amortization tool.\n\n");
  95.        fprintf(fp,"Principal:  $%.2f\n", amt);
  96.         fprintf(fp,"Interest rate:  %3.3f%%\n", rate);
  97.        fprintf(fp,"Term of loan in years:  %.2f\n", term);
  98.        fprintf(fp,"Monthly payment:  $%.2f\n", pmt);
  99.         fprintf(fp,"Total interest charged:  $%.2f\n", ic);
  100.        fprintf(fp,"Total cost on loan:  $%.2f\n", fv);
  101.        
  102.       
  103.       fprintf(fp,"\nMonth\tInterest\tPrincipal\tBalance\n");
  104.   
  105.        /* start of loop to print amortization schedule */
  106.   
  107.        mnbr=mbeg;
  108.        for (i=1; i<=k; i++) {
  109.           month = i;
  110.           exp = amt*(r/12.0);
  111.           yrint=yrint+exp;
  112.           prin = pmt-exp;
  113.           amt = amt-prin;
  114.           mnbr++;
  115.           fprintf(fp,"%d\t %.2f\t\t %.2f\t\t %.2f\n", mnbr-1, exp, prin, amt);
  116.           if (mnbr > 12 ) {
  117.           fprintf(fp,"\t\tInterest paid for year %d is %.2f\n\n",yr,yrint);
  118.           yr++;
  119.           yrint=0;
  120.           mnbr=1;
  121.               }
  122. }
  123.         
  124.         if (mnbr !=1 && mnbr != 13) {
  125.                                fprintf(fp,"\t\tInterest paid for year %d is %.2f\n\n",yr,yrint);
  126.                                                      }
  127.         
  128.          fclose(fp);
  129.         
  130. }
  131.